<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false"%>
<%@ include file="/WEB-INF/view/common/loginCheck.jspf" %>
<div class="easyui-panel" data-options="fit:true,footer:'#functionEditBtns'" style="padding:10px;">
    <form id="functionSelectionForm" method="post">
		 <input type="hidden" id="roleId" name="roleId" value="${role.id}" />
		 <input type="hidden" id="functionIds" name="functionIds" value="" />
		 <ul id="functionTreeListFromDlg"></ul>
	</form>
</div>
<div id="functionEditBtns" style="text-align:right;padding:2px;">
	<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="addFunction();">保存</a>
	<!-- <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="closeDlg();">关闭</a> -->
</div>
<script type="text/javascript">
	$(function () {
		$('#functionTreeListFromDlg').tree({
		    url:'${ROOT}/account/function/tree?roleId=' + $('#roleId').val() + '&_rt=' + new Date().getTime(),
		    checkbox: true,
		    onlyLeafCheck: false,
		    onClick: function (node) {
		    	
		    },
		    formatter:function(node) {
                var s = node.text;
                if (node.children){
                    s += '<span style="color:blue">[' + node.children.length + ']</span>';
                }
                return s;
            }
		});
		
		$('#functionSelectionForm').form({
		    url:'${ROOT}/account/role/authorize?_rt=' + new Date().getTime(),
		    ajax: true,
		    onSubmit: function() {
		    	var nodes = $('#functionTreeListFromDlg').tree('getChecked');
	            var functionIds = '';
	            for(var i = 0; i < nodes.length; i++){
	                if (functionIds != '') functionIds += ',';
	                functionIds += nodes[i].id;
	            }
	            $('#functionIds').val(functionIds);
		    	return $(this).form('enableValidation').form('validate');
		    },
		    success:function(data) {
		    	var json = eval('(' + data + ')');
				if (json.success) {
					APP.message('保存成功');
		        	$('#functionSelectionDlg').dialog('close');
		        } else {
		        	 APP.error(json.msg);
		        }
		    }
		});
	});
	
	function addFunction() {
		$('#functionSelectionForm').submit();
	}
	function closeDlg() {
		$('#functionSelectionDlg').dialog('close');
	}
</script>